package com.aeye.mbr.upms.rpc.service.impl;

import com.aeye.mbr.cache.CacheService;
import com.aeye.mbr.upms.common.constant.UpmsCacheConstants;
import com.aeye.mbr.upms.dao.model.UpmsDict;
import com.aeye.mbr.upms.rpc.api.UpmsDictService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* Description:字典获取
* @author zhouchuanbo
* @date 上午 11:31 2018/3/23 0023
*/
public class UpmsDictCacheServiceImpl implements CacheService {

    @Autowired
    private UpmsDictService dictService;

    @Override
    public String getCacheName() {
        return UpmsCacheConstants.UPMS_DICT_CACHE;
    }

    @Override
    public Object getCacheObject() {
        HashMap<String,List<UpmsDict>> maps = new HashMap<String,List<UpmsDict>>();
        List<UpmsDict> list = dictService.getAllDicts();
        for(UpmsDict dict:list){
            //字段名
//				String fieldName = dict.getFieldName();
            String fieldName = dict.getAppFieldName();
            if (fieldName == null || fieldName.trim().length() == 0) {
                continue;
            }
            fieldName = fieldName.toUpperCase();
            if (maps.containsKey(fieldName)) {
                List<UpmsDict> items = (List<UpmsDict>) maps.get(fieldName);
                //字段值
                String fieldValue = dict.getFieldValue();
                if (fieldValue != null && fieldValue.trim().length() > 0) {
                    items.add(dict);
                }
            } else {
                List<UpmsDict> items = new ArrayList<UpmsDict>();
                String fieldValue = dict.getFieldValue();
                if (fieldValue != null && fieldValue.trim().length() > 0) {

                    items.add(dict);
                }
                maps.put(fieldName, items);
            }
        }
        return maps;
    }
}
